<?xml version="1.0" encoding="UTF-8" ?>
<%@page import="org.apache.tomcat.util.http.Cookies"%>
<%@page import="org.apache.jasper.tagplugins.jstl.core.ForEach"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" errorPage="login.jsp" %>
<%
	request.setCharacterEncoding("UTF-8");//设置页面编码
	String username = "";//用户名
	int visitTimes = 0;//访问次数
	Cookie[] cookies = request.getCookies();
	for (int i = 0; cookies != null && i < cookies.length; i++) {
		Cookie cookie = cookies[i];
		if("username".equals(cookie.getName())){
			username = cookie.getValue();
		}else if("visitTimes".equals(cookie.getValue())){
			visitTimes = Integer.parseInt(cookie.getValue());
		}
		if(username == null || username.trim().equals("")){
			throw new Exception("您没登录哈！");
		}
		Cookie visitTimesCookie = new Cookie("visitTimes",Integer.toString(++visitTimes));
		response.addCookie(visitTimesCookie);
	}
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Cookie操作</title>
</head>
<body>
您的账户：<%= username %>，访问次数：<%=visitTimes %>
<input type="button" value="刷新" onclick="location='<%=request.getRequestURI() %>?ts='+ new Date().getTime();" />
</body>
</html>